package com.shengwei.truck.driver.view.adapter.task;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.shengwei.truck.driver.R;
import com.shengwei.truck.driver.bean.MyConstant;
import com.shengwei.truck.driver.bean.task.OrderItem;
import com.shengwei.truck.driver.bean.task.TaskItem;
import com.shengwei.truck.driver.databinding.TaskItemListBinding;
import com.shengwei.truck.driver.utils.DateTimeUtils;
import com.shengwei.truck.driver.view.adapter.BaseAdapter;

import java.util.List;

/**
 * 任务列表适配器
 */
public class TaskListAdapter extends BaseAdapter<TaskListAdapter.ViewHolder> {
    private static final int ITEM_EXCEPTION =  4;
    private List<TaskItem> mDataList;
    private int mTaskState = -1;
    private final int ITEM_NORMAL = 0;

    public TaskListAdapter(Context context,int taskState) {
        super(context);
        mTaskState = taskState;
    }
    /**
     * 更新任务列表
     * @param dataList
     */
    public void notifyDataSetChanged(List<TaskItem> dataList) {
        this.mDataList = dataList;
        super.notifyDataSetChanged();
    }
    @Override
    public void notifyOrderItemChange(List<OrderItem> dataList) {

    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        TaskItemListBinding taskItemListBinding = DataBindingUtil.inflate(getInflater(),R.layout.task_item_list,parent,false);
        if(viewType == ITEM_EXCEPTION) {
            int colorRed = parent.getContext().getResources().getColor(R.color.exception_red);
            taskItemListBinding.tvPublishTime.setTextColor(colorRed);
        }
        return new ViewHolder(taskItemListBinding);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        TaskItemListBinding viewDataBinding = (TaskItemListBinding) holder.viewDataBinding;
        TaskItem taskItem = mDataList.get(position);
        //taskItem.uaName = MyConstant.testCustomName;
        //使用DataBinding来填充数据
         viewDataBinding.setTaskItem(taskItem);
        //以下两个字段要单独赋值，因为订单列表项和任务列表项公用的是一个布局，显示的内容有差别
        viewDataBinding.tvTaskNoLabel.setText(R.string.task_num2);
        viewDataBinding.tvTaskNoValue.setText(taskItem.taskNo);
      //  viewDataBinding.tvUnloadCity.setText(MyConstant.testCustomName);

        int share = taskItem.share;  //0未共享 1已共享 2待确认
        int transfer = taskItem.transfer;  //0未转派 1已转派 2待确认
        viewDataBinding.imvForward.setVisibility(View.GONE);

        //共享小标签：发起人和目标人的任务列表都显示
        if(share == 1 || share == 2){
            viewDataBinding.imvForward.setVisibility(View.VISIBLE);
            viewDataBinding.imvForward.setImageResource(R.drawable.label_share);
        }
        //转派小标签：发起人显示 “转派中”，目标人显示“转派”
        if(transfer == 1 || transfer == 2){
            viewDataBinding.imvForward.setVisibility(View.VISIBLE);
            if(transfer == 1) {
                viewDataBinding.imvForward.setImageResource(R.drawable.label_transfer);
            }
            else{
                viewDataBinding.imvForward.setImageResource(R.drawable.label_transferring);
            }
        }
        //“已转派” 列表与异常任务列表，不显示分享（转派）标签,4代表异常任务
        if(mTaskState == -1 || mTaskState == 4 || mTaskState == 5) {
            viewDataBinding.imvForward.setVisibility(View.GONE);
        }
        if(mTaskState == 5){
            viewDataBinding.imvCompleteFlag.setVisibility(View.VISIBLE);
        }

        //榜单审核失败,显示错误提示
        if(mTaskState == 4) {
            viewDataBinding.tvPublishTime.setText(R.string.poundsError);
            viewDataBinding.imvException.setVisibility(View.VISIBLE);
        }
        else{ //其它状态的任务仍就显示发布时间
            viewDataBinding.tvPublishTime.setText(DateTimeUtils.timeStampToDateString(taskItem.createTime));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(mTaskState == 4) {//表示当前TAB列表为“已转派”列表
            return ITEM_EXCEPTION;
        }
        return ITEM_NORMAL;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ViewDataBinding viewDataBinding;
        public ViewHolder(ViewDataBinding viewDataBinding) {
            super(viewDataBinding.getRoot());
            this.viewDataBinding = viewDataBinding;
        }
    }
}
